/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbchess.units.pieces;

import bbchess.units.ChessPiece;
import bbchess.units.ChessSquare;
import bbchess.util.ImageLoader;

/**
 *
 * @author Tyler
 */
public class Queen extends ChessPiece {
    public Queen(String color, ChessSquare position) {
        initPiece(color, position);
        setUnit("Q");

        if (color.equals("black")) {
            setSprite(ImageLoader.getbQueenImage());
        } else {
            setSprite(ImageLoader.getwQueenImage());
        }
    }

    @Override
    public void calculateLegalMoves() {
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, 0, 1, false));
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, 0, -1, false));
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, 1, 0, false));
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, -1, 0, false));
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, 1, 1, false));
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, 1, -1, false));
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, -1, 1, false));
        addLegalMoves(board.calculatePath(getPosition().getPosition(), 8, -1, -1, false));
    }

    @Override
    public void calculateCaptureMoves() {
        if (moveContainsKing(
                addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 1, 1)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, 1, 1, false));
        }

        if (moveContainsKing(
                addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 1, -1)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, 1, -1, false));
        }

        if (moveContainsKing(
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, -1, 1)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, -1, 1, false));
        }

        if (moveContainsKing(
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, -1, -1)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, -1, -1, false));
        }

        if (moveContainsKing(
                addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 0, 1)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, 0, 1, false));
        }

        if (moveContainsKing(
                addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 0, -1)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, 0, -1, false));
        }

        if (moveContainsKing(
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 1, 0)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, 1, 0, false));
        }

        if (moveContainsKing(
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 8, -1, 0)))) {
            setKingCapturePath(board.calculatePath(getPosition().getPosition(), 8, -1, 0, false));
        }
    }

    @Override
    public void calculateKingPinMoves(ChessPiece piece) {

        setPinningKing(false);
        
        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 1, 1, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, 1, 1, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }

        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 1, -1, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, 1, -1, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }

        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, -1, 1, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, -1, 1, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }

        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, -1, -1, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, -1, -1, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }

        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 0, 1, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, 0, 1, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }

        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 0, -1, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, 0, -1, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }

        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, 1, 0, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, 1, 0, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }

        if (moveContainsKing(board.calculateCapture(getPosition().getPosition(), getColor(), 8, -1, 0, piece))) {
            setKingPinPath(board.calculatePath(getPosition().getPosition(), 8, -1, 0, false));
            addKingPinPath(getPosition().getPosition());
            setPinningKing(true);
        }
    }

    @Override
    public void calculateThreatenedMoves() {
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, 0, 1, true));
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, 0, -1, true));
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, 1, 0, true));
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, -1, 0, true));
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, 1, 1, true));
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, 1, -1, true));
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, -1, 1, true));
        addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 8, -1, -1, true));
    }
}
